package com.example.demo.helper;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest;
import com.example.demo.model.Notify;
import com.taobao.api.ApiException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * 钉钉通知辅助工具
 */
@Component
public class DingDingHelper implements NotifyHelper {

    @Value("${notify.dingding}")
    private String dingdingUrl;
    /**
     * 发送消息
     * @param notify 通知
     * @return 发送是否成功
     */
    @Override
    public boolean sendNotify(Notify notify) {
        String notifyType = notify.getNotifyType();
        if ("text".equals(notifyType)){
            return sendText(notify);
        }else if ("link".equals(notifyType)){
            return sendLink(notify);
        }else if ("markdown".equals(notifyType)){
            return sendMarkdown(notify);
        }
        return false;
    }

    /**
     * 发送文字通知
     * @param notify 通知
     * @return 发送是否成功
     */
    private boolean sendText(Notify notify){
        // 1. 配置钉钉客户端，dingdingUrl即群自定义聊天机器人的Webhook
        DingTalkClient client = new DefaultDingTalkClient(dingdingUrl);
        // 2. request表示整个消息请求
        OapiRobotSendRequest request = new OapiRobotSendRequest();
        // 3. 请求设置消息类别
        request.setMsgtype("text");

        // 4. 设置消息内容
        OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
        text.setContent(notify.getText());
        request.setText(text);

        // 5. 设置钉钉@功能
        OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
        at.setIsAtAll(true);
        request.setAt(at);
        try {
            // 6. 发送消息请求
            client.execute(request);
        } catch (ApiException e) {
            e.printStackTrace();
        }
        return true;
    }

    /**
     * 发送链接通知
     * @param notify 通知
     * @return 发送是否成功
     */
    private boolean sendLink(Notify notify){
        DingTalkClient client = new DefaultDingTalkClient(dingdingUrl);
        OapiRobotSendRequest request = new OapiRobotSendRequest();
        request.setMsgtype("link");

        OapiRobotSendRequest.Link link = new OapiRobotSendRequest.Link();
        link.setMessageUrl(notify.getLink());
        link.setPicUrl(notify.getPicUrl());
        link.setTitle(notify.getTitle());
        link.setTitle(notify.getText());
        request.setLink(link);

        try {
            client.execute(request);
        } catch (ApiException e) {
            e.printStackTrace();
        }

        return true;
    }

    /**
     * 发送Markdown通知
     * @param notify 通知
     * @return 发送是否成功
     */
    private boolean sendMarkdown(Notify notify){
        DingTalkClient client = new DefaultDingTalkClient(dingdingUrl);
        OapiRobotSendRequest request = new OapiRobotSendRequest();
        request.setMsgtype("markdown");

        OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown();
        markdown.setTitle(notify.getTitle());
        markdown.setText(notify.getText());
        request.setMarkdown(markdown);

        try {
            client.execute(request);
        } catch (ApiException e) {
            e.printStackTrace();
        }

        return true;
    }
}
